package com.wanjia.estate.service.bdservice.FjtService;

import com.wanjia.estate.pojo.entity.Fjtwyjdetzfqrs;
import com.wanjia.estate.pojo.entity.Sjd;
import com.wanjia.estate.properties.FileTemplateProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Calendar;
import java.util.Date;

@Service
public class ExcelReaderWriterFjtService {
    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    public String ExcelReaderWriter(Fjtwyjdetzfqrs fjtwyjdetzfqrs, String folder) {
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getFjtwyjdetzfqrs());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            Row row15 = sheet.getRow(15);
            row15.getCell(3).setCellValue(fjtwyjdetzfqrs.getSfzh());

            Row row16 = sheet.getRow(15);
            row16.getCell(10).setCellValue(fjtwyjdetzfqrs.getDai());


            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，需要加1
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            Row row20 = sheet.getRow(19);
            row20.getCell(4).setCellValue(year);
            row20.getCell(6).setCellValue(month);
            row20.getCell(8).setCellValue(day);

            FileOutputStream outputStream = new FileOutputStream(folder + "/" + "fjt.xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder + "/" + "fjt.xlsx");
        return file.getPath();
    }
}
